Attribute VB_Name = "Program"
Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Private Declare Function GetDlgItemText Lib "user32" Alias "GetDlgItemTextA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
         ByVal wParam As Long, _
         ByVal lParam As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Private Const WM_CLOSE = &H10
Private Const WM_DESTROY = &H2

Private WindowsToPop As New Collection
Private EventLogger As New EventLogger
Private Description As String
Private XmlConfigFile As DOMDocument

Public Sub Main()

    
    On Error GoTo ErrorHandler
    
    Set XmlConfigFile = LoadXMLDocument(App.Path & "\Popper.xml")
    GetWindowsFromConfig XmlConfigFile
    
    
    Dim lngHWnd As Long
    Dim lngHWndChild As Long
    Dim intCounter As Integer
    
    'Iterate over all Windows from config file
    For intCounter = 1 To WindowsToPop.Count
        Dim Window As WindowToPop
        Set Window = WindowsToPop.Item(intCounter)
        lngHWnd = FindWindow(vbNullString, Window.Title)
        Debug.Print Window.Title & ":" & lngHWnd
        
        Sleep Window.TimeSleep
        
        'Find a Window currently running whose Title is listed in Config
        If lngHWnd <> 0 Then
            
            Dim lngIndex As Long
            Dim lngDlgItem As Long
            Dim lngTextLength As Long
            Dim strText As String
        
            For lngIndex = 0 To 1000000
                lngDlgItem = GetDlgItem(lngHWnd, lngIndex)
                If lngDlgItem <> 0 Then
                    strText = Space(Len(Window.Text) + 1)
                    lngTextLength = GetDlgItemText(lngHWnd, lngIndex, strText, Len(Window.Text) + 1)
                    If lngTextLength <> 0 Then
                        
                        'Dim byteArray() As Byte
                        'byteArray = StrConv(strText, vbFromUnicode)
                        
                        strText = Trim(Replace(strText, Chr(0), ""))
                        
                        ' If the text matches
                        If Window.Text = strText Then
                            
                            Description = "Closing - Title:" & Window.Title & "Body: " & Window.Text
                            EventLogger.LogEvent Description, eInformation, App.EXEName
                            
                            Call PostMessage(lngHWnd, WM_CLOSE, 0&, 0&)
                            Call PostMessage(lngHWnd, WM_DESTROY, 0&, 0&)
                                                       
                            Description = "Closed - Title: " & Window.Title & " Body: " & Window.Text
                            EventLogger.LogEvent Description, eSuccess, App.EXEName
                            
                        End If
                    End If
                End If
            Next
        
        End If
    Next
    
ErrorHandler:
    If Err.Number <> 0 Then
        Description = "Main: " & Err.Description
        EventLogger.LogEvent Description, eError, App.EXEName
    End If
        
End Sub

Private Function LoadXMLDocument(ByVal url As String) As DOMDocument
On Error GoTo ErrorHandler

    Dim localXmlConfigFile As New DOMDocument

    localXmlConfigFile.async = False
    localXmlConfigFile.Load (url)
    localXmlConfigFile.Validate
           
    If (localXmlConfigFile.parseError <> 0) Then
        Err.Raise localXmlConfigFile.parseError.errorCode, localXmlConfigFile.parseError.srcText, localXmlConfigFile.parseError.reason
    End If
    
   Set LoadXMLDocument = localXmlConfigFile
   Set localXmlConfigFile = Nothing
    
ErrorHandler:
    If Err.Number <> 0 Then
       Description = "LoadXMLDocument: " & Err.Description
       EventLogger.LogEvent Description, eError, App.EXEName
    End If
      
End Function

Public Sub GetWindowsFromConfig(ByRef XMLDoc As DOMDocument)
    On Error GoTo ErrorHandler
    
    Dim sections As IXMLDOMNodeList
    Dim sectionNode As IXMLDOMNode
    Set sections = XMLDoc.selectNodes("//Window")
    
    
    For Each sectionNode In sections
        Dim temp As WindowToPop
        Set temp = New WindowToPop
        temp.Title = CStr(sectionNode.selectSingleNode("Title").Text)
        temp.Text = CStr(sectionNode.selectSingleNode("Text").Text)
        temp.TimeSleep = CInt(sectionNode.selectSingleNode("SleepTime").Text)
        WindowsToPop.Add temp
           
    Next
ErrorHandler:
    If Err.Number <> 0 Then
        
        Description = "GetWindowsFromConfig: " & Err.Description
        EventLogger.LogEvent Description, eError, App.EXEName
    End If
    
   
End Sub






